package com.youlu.campus.admin.live.service;

import com.youlu.campus.admin.utils.JsonUtil;
import com.youlu.campus.base.live.entity.LiveAccount;
import lombok.extern.slf4j.Slf4j;
import net.polyv.live.bean.model.ChannelAccount;
import net.polyv.live.bean.request.account.PLChannelAccountsGetRequest;
import net.polyv.live.bean.request.channel.PLChannelCreateRequest;
import net.polyv.live.bean.result.PLCommonListResult;
import net.polyv.live.bean.result.channel.PLChannelCreateResult;
import net.polyv.live.service.PLChannelAccountService;
import net.polyv.live.service.PLChannelService;
import net.polyv.live.service.impl.PLChannelAccountServiceImpl;
import net.polyv.live.service.impl.PLChannelServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 保利威云直播客户端接口
 * @author: YangJ
 * @date: 2020/11/11 14:22
 */
@Slf4j
@Component
public class PlChannelClient {

    @Value("${polyv.appId}")
    private String appId;

    @Value("${polyv.appSecret}")
    private String appSecret;

    @Value("${polyv.userId}")
    private String userId;

    /**
     * 创建直播频道
     * @return
     */
    public PLChannelCreateResult createLive(String name,String pwd){
        //  创建频道的创建请求对象（并传入直播账号的APPID和APPSECRET）
        PLChannelCreateRequest request = new PLChannelCreateRequest(appId, appSecret);
        //  设置用户ID
        request.setUserId(userId);
        //  设置密码
        request.setChannelPasswd(pwd);
        //  设置名称
        request.setName(name);
        //  设置场景
        request.setScene("ppt");
        //  是否为无延时直播，Y 表示开启，默认为N
        request.setPureRtcEnabled("Y");
        //  频道接口对象
        PLChannelService plChannelService = new PLChannelServiceImpl();
        //  调用频道接口对象的创建频道方法获取请求结果对象
        PLChannelCreateResult result = plChannelService.createChannel(request);
        //  成功响应
        if(null != result && 200 == result.getCode()) {
            //业务流程
            log.info("云直播接口返回结果:{}", JsonUtil.toJSON(result));
            return result;
        }
        log.info("云直播接口返回错误结果:{}", JsonUtil.toJSON(result));
        return null;
    }

    /**
     * 查询直播频道状态
     * @param stream
     * @return
     */
    public String liveStatus(String stream){

        PLChannelService plChannelService = new PLChannelServiceImpl();
        String liveStatus = plChannelService.getChannelLiveStatus(stream);

        return liveStatus;
    }

    public List<LiveAccount> liveChannelAccount(int channelId){
        PLChannelAccountService pca = new PLChannelAccountServiceImpl();

        PLChannelAccountsGetRequest request = new PLChannelAccountsGetRequest(appId, appSecret);
        PLCommonListResult<ChannelAccount> result = pca.getAccounts(channelId, request);
        //  成功响应
        if(null != result && 200 == result.getCode()) {
            //业务流程
            log.info("云直播子频道账号接口返回结果:{}", JsonUtil.toJSON(result));
            List<ChannelAccount> accounts = result.getT();
            if(accounts != null && accounts.size() >0){
                List<LiveAccount> list = accounts.stream().map(account ->{
                    LiveAccount la = new LiveAccount();
                    la.setAccount(account.getAccount());
                    la.setNickname(account.getNickname());
                    la.setPasswd(account.getPasswd());
                    la.setActor(account.getActor());
                    return la;
                }).collect(Collectors.toList());
                return list;
            }
        }
        log.info("云直播子频道账号返回错误结果:{}", JsonUtil.toJSON(result));
        return null;
    }
}
